package com.kelan.android.app.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kelan.android.app.entity.AjaxResult;
import com.kelan.android.app.entity.BzJcryBspRelEntity;
import com.kelan.android.app.entity.BzJcryEntity;
import com.kelan.android.app.entity.BzJczjEntity;
import com.kelan.android.app.entity.EasyUIDataGridResult;
import com.kelan.android.app.entity.LoginEntity;
import com.kelan.android.app.service.BzJcryBspRelService;
import com.kelan.android.app.service.BzJcryService;
import com.kelan.android.app.service.BzJczjService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "登录功能")
@Controller
public class LoginController {

	@Autowired
	private BzJcryService bzJcryService;
	
	@Autowired
	private BzJczjService bzJczjService;
	
	@Autowired
	private BzJcryBspRelService bzJcryBspRelService;
	
	@PostMapping("login")
	@ResponseBody
	public AjaxResult login(HttpServletRequest req, LoginEntity loginEntity) {
		HttpSession session = req.getSession();
		if ("hkadmin".equals(loginEntity.getId())) {
			session.setAttribute("username", "hkadmin");
			return AjaxResult.success();
		}
		if (StringUtils.isBlank(loginEntity.getId())) {
			return AjaxResult.error("账号密码错误");
		}
		BzJcryEntity entity = bzJcryService.selectById(loginEntity.getId());
		if (entity == null) {
			return AjaxResult.error("账号密码错误");
		}
		session.setAttribute("username", loginEntity.getId());
		return AjaxResult.success();
	}
	
	@ApiOperation(value = "用户登录接口", nickname = "用户登录接口", notes = "入参是对象")
	@PostMapping("/mobile/login")
	@ResponseBody
	public AjaxResult mobileLogin(HttpServletRequest req, LoginEntity loginEntity) {
		HttpSession session = req.getSession();
		if ("hkadmin".equals(loginEntity.getId())) {
			session.setAttribute("inspectId", "hkadmin");
			return AjaxResult.success();
		}
		BzJcryBspRelEntity entity = bzJcryBspRelService.selectBzJcryBspRelByUserId(loginEntity.getId());
		if (entity == null) {
			return AjaxResult.error("账号密码错误");
		}
		session.setAttribute("inspectId", entity.getUserId());
		session.setAttribute("inspectName", entity.getUserName());//
		System.out.println("deptCode=======>" + entity.getDeptCode());
		session.setAttribute("deptCode", entity.getDeptCode());
		return AjaxResult.success();
	}
	
	@RequestMapping("user")
	public String indexhtml() {
		return "user.html";
	}
	
	/**
	 * @Title: getStrList
	 * @Description: 查询监管企业信息 select * from BZ_SCZTXX T where 1=1
	 * @Author 刘仁
	 * @DateTime 2020年2月27日 下午12:40:23
	 * @return
	 */
	@GetMapping("bzjcry")
	@ResponseBody
	public EasyUIDataGridResult getStrList(@RequestParam(value="page",required = true, defaultValue = "1") Integer pageNum ,
            @RequestParam(value="rows",required = true, defaultValue = "10") Integer rows, BzJcryEntity bzJcryEntity){
		PageHelper.startPage(pageNum , rows);
		List<BzJcryEntity> list = bzJcryService.selectBzJcryList(bzJcryEntity);
		PageInfo<BzJcryEntity> personPageInfo = new PageInfo<>(list);
		EasyUIDataGridResult dataGridResult = new EasyUIDataGridResult();
		dataGridResult.setTotal(Integer.parseInt(personPageInfo.getTotal()+""));
		dataGridResult.setRows(list);
		return dataGridResult;
	}
	
	/**
	 * @Title: getStrList
	 * @Description: 查询检查专家库
	 * @Author 刘仁
	 * @DateTime 2020年2月27日 下午12:40:23
	 * @return
	 */
	@GetMapping("bzjczj")
	@ResponseBody
	public EasyUIDataGridResult getBzjczjList(@RequestParam(value="page",required = true, defaultValue = "1") Integer pageNum ,
            @RequestParam(value="rows",required = true, defaultValue = "10") Integer rows, BzJczjEntity bzJczjEntity){
		PageHelper.startPage(pageNum , rows);
		List<BzJczjEntity> list = bzJczjService.selectBzJczjList(bzJczjEntity);
		PageInfo<BzJczjEntity> personPageInfo = new PageInfo<>(list);
		EasyUIDataGridResult dataGridResult = new EasyUIDataGridResult();
		dataGridResult.setTotal(Integer.parseInt(personPageInfo.getTotal()+""));
		dataGridResult.setRows(list);
		return dataGridResult;
	}
}
